/**
 * 
 */
package com.heer.RD.projecthour.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.model.Model;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;

import com.heer.RD.projecthour.pojo.ProjectDTO;
import com.heer.RD.projecthour.pojo.UserInfoDTO;
import com.heer.RD.util.SecurityFacade;
import com.heer.joss.persistence.IBaseDAO;

/**
 * @author ZhaoZehui
 *
 */
public class DeptProjChoice extends ListChoice {

	private Map<String, String> NAMES = new HashMap<String, String>();
	private List<String> projIds = new ArrayList<String>();
	
	public DeptProjChoice(String id, int maxRow) {
		super(id);
		setModel(new Model<String>());
		setMaxRows(maxRow);
		setNullValid(true);
		setChoices(projIds);
	}
	
	public void init(Map<String, String> projMapList) {
		
		Assert.notNull(projMapList);
		NAMES.clear();
		projIds.clear();
		
		for (Iterator iterator = projMapList.keySet().iterator(); iterator.hasNext();) {
			String projId = (String) iterator.next();
			projIds.add(projId);
			NAMES.put(projId, projMapList.get(projId));
		}
		
		
		IChoiceRenderer render = new ChoiceRenderer() {
			public Object getDisplayValue(Object object) {
				return NAMES.get(object);
			}
			public String getIdValue(Object object, int index)	{
				if(index >= 0)
					return String.valueOf(projIds.get(index));
				else
					return "";
			}
		};

		this.setChoices(projIds);
		this.setChoiceRenderer(render);
	}
	
	public String getProjName(String projId) {
		String projName = NAMES.get(projId);
		projName = StringUtils.replace(projName, "…", "");
		projName = StringUtils.replace(projName, "├", "");
		projName = StringUtils.replace(projName, "─", "");
		return projName;
	}
}
